package com.smiletrain.activities;

import java.util.ArrayList;
import java.util.List;

import com.smiletrain.R;
import com.smiletrain.activities.base.AbstractActivity;
import com.smiletrain.base.Caso;
import com.smiletrain.base.Usuario;
import com.smiletrain.util.AdminCasos;
import com.smiletrain.util.AdminUsuarios;
import com.smiletrain.util.Constantes;
import com.smiletrain.util.Util;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class EnviarEmailActivity extends AbstractActivity {
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.email_view);
		super.setFootLayout();

		ImageView enviarEmail = (ImageView) findViewById(R.id.bEnviarEmail);
		
		enviarEmail.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String[] emailTo;
				List<String> filePaths = new ArrayList<String>();
				
				TextView viewcoment = (TextView) findViewById(R.id.txtComentarios);
				
				TextView email = (TextView) findViewById(R.id.txtEmail);
				CharSequence semail = email.getText();
				String emailx = semail.toString();
				emailTo = new String[]{ emailx };
				
				String comentarios = viewcoment.getText().toString();
				
				Caso c = AdminCasos.instance.getCasoActual();
				
				String asunto = Constantes.EMAIL_ASUNTO + c.getId();
				
				String aux = "";
				
				if(!(aux = c.getPreFotos().getPathImage1()).equals(""))
					filePaths.add(aux);
				
				if(!(aux = c.getPreFotos().getPathImage2()).equals(""))
					filePaths.add(aux);
				
				if(!(aux = c.getPostFotos().getPathImage1()).equals(""))
					filePaths.add(aux);
				
				if(!(aux = c.getPostFotos().getPathImage2()).equals(""))
					filePaths.add(aux);
				
				if(AdminCasos.validarEnviarEmail(emailTo,comentarios, EnviarEmailActivity.this)){
					
					pd = ProgressDialog.show(EnviarEmailActivity.this, "Enviando", "Por favor espere...", true, false);

					Thread thread = new Thread(new SendEmailThread(emailTo, asunto));
					
					thread.start();

				}
				
			}
			
		});
                
	}
	
	@Override
	public void updateCaso() {
		
	}
	
	
	private class SendEmailThread implements Runnable {		
		
		public SendEmailThread(String[] emailTo, String subject) {
			this.emailTo = emailTo;
			this.asunto = subject;
		}
		
		@Override
		public void run() {

			String email = "";
			for(String em : emailTo) email += em + ",";
			
			if(Util.sendImage(email, asunto, usuario, caso) == Constantes.OK){
				handler.sendEmptyMessage(Constantes.OK);
			} else {
				handler.sendEmptyMessage(Constantes.NO);
			}
			
		}
		
		private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
            	pd.dismiss();

            	switch(msg.what){
            		case Constantes.OK:
            			Toast.makeText(EnviarEmailActivity.this, Constantes.MSG_VAL03_V10, Toast.LENGTH_SHORT).show();
            			Intent intent = new Intent(EnviarEmailActivity.this, WellcomeActivity.class);
        				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        				startActivity(intent);
        				overridePendingTransition(R.anim.in_anim, R.anim.back_anim);
	            		break;
	            	case Constantes.NO:
	            		Toast.makeText(EnviarEmailActivity.this, Constantes.MSG_VAL04_V10, Toast.LENGTH_SHORT).show();
	            		Toast.makeText(EnviarEmailActivity.this, Util.error, Toast.LENGTH_SHORT).show();
	            		break;
	        		default:
	            		Toast.makeText(EnviarEmailActivity.this, Constantes.MSG_VAL05_V10, Toast.LENGTH_SHORT).show();
	            		Toast.makeText(EnviarEmailActivity.this, Util.error, Toast.LENGTH_SHORT).show();
        			
            	}
            	
            }
		};
    
		private String emailTo[], asunto;

	}
	
	private ProgressDialog pd;
	
	private Usuario usuario = AdminUsuarios.instance.getUsuario();
	
}
